<?php
/**
 * Created by Command.
 * User: Vito
 */

namespace app\admin\curd\pay_record;

use app\common\curd\Save;
use app\model\PayRecord;
use app\exception\MessageException;
use app\model\Room;
use app\model\WaterElePrice;
use app\logic\PayRecordLogic;
class PayRecordSave extends Save
{
    //验证器
    protected $validate = [];
    //追加数据
    protected $appendParams = [];
    //允许新增的数据字段
    protected $field = [];

    
    protected function saveMiddleware($next,$params){

        $params['fee_date']  = strtotime($params['fee_date']);

        $payRecord = new PayRecord;
        $room = new Room;
        $WaterEle = new WaterElePrice;
        $payRecordLogic = new PayRecordLogic;
        $month = date("Y-m",$params['fee_date']);

        //指定月份第一天的时间戳
        $lastMonthfirstDay = strtotime(date("Y-m-d 00:0:00", strtotime("first day of {$month}")));

        // 指定月份最后一天的时间戳
        $lastMonthLastDay = strtotime(date("Y-m-d 23:59:59", strtotime("{$month} +1 month -1 day")));

        
        $findWhere[]   = ['fee_date','between',"$lastMonthfirstDay, $lastMonthLastDay",'AND'];
        $findWhere[]   = ['room_id','=',$params['room_id']];
        
        // 获取房间该月是否已生成缴费记录
        $findRecord = $payRecord->getPayRecord($findWhere);
        if($findRecord){
            throw new MessageException('该房间'. $month.'已生成缴费记录');
        }
        $recordWhere['room_id'] = $params['room_id'];

        // 获取房间最新的缴费记录
        $recordData = $payRecord->getPayRecord($recordWhere);
        // if(!$recordData){
        //     throw new MessageException('该房间暂无初始化水电量，请先导入初始化数据');
        // }

        // 获取房间的租金
        $roomPriceWhere['id'] = $params['room_id'];
        $roomPrice = $room->getRoomPrice($roomPriceWhere);

        // 获取房间的物业费
        $propertyPrice = $room->getPropertyPrice($roomPriceWhere);
        // if (!$roomPrice) {
        //     throw new MessageException('找不到该房间的租金，请检查该房型是否存在');
        // }

        if(!$recordData){
            $params['m_water'] =  $params['water'] - $params['oldWater'];
            $params['m_electricity']   =   $params['electricity'] - $params['oldEle'];
        }else{
            $params['m_water'] =  $params['water'] - $recordData['water'];
            $params['m_electricity']   =  $params['electricity'] - $recordData['electricity'];
        }

        // halt($params);
        // 获取水电单价
        $waterPrice = $WaterEle->getWaterElePrice(0);
        $elePrice   = $WaterEle->getWaterElePrice(1);

        $waterPrice = $waterPrice->toArray();
        $elePrice = $elePrice->toArray();
        // 初始化水费值
        $params['water_price'] = 0;
        $m_water  = $params['m_water'];

        foreach ($waterPrice as $key => $value) {
            // if($key == 0){
                if(preg_match("/^[1-9][0-9]*$/",($m_water - $value['top']))){
                    $params['water_price'] = $params['water_price'] + ($value['top']- $value['bottom']) * $value['unit_price'];
                }else{
                if (preg_match("/^[1-9][0-9]*$/", ($m_water - $value['bottom']))) {
                    $params['water_price'] = $params['water_price'] + ($m_water - $value['bottom']) * $value['unit_price'];
                }
            }
        }
        //初始化电费值
        $params['electricity_price'] = 0;
        $m_ele  = $params['m_electricity'];

        foreach ($elePrice as $key => $value) {
            // if($key == 0){
            if (preg_match("/^[1-9][0-9]*$/", ($m_ele - $value['top']))) {
                $params['electricity_price'] = $params['electricity_price'] + ($value['top'] - $value['bottom']) * $value['unit_price'];
            } else {
                if (preg_match("/^[1-9][0-9]*$/", ($m_ele - $value['bottom']))) {
                    $params['electricity_price'] = $params['electricity_price'] + ($m_ele - $value['bottom']) * $value['unit_price'];
                }
            }
        }
        // // 获取房间的当月的水电费
        // $params['water_price'] = $params['m_water'] * $waterElePrice['water_price'];
        // $params['electricity_price'] = $params['m_electricity'] * $waterElePrice['electricity_price'];
        
        //共计金额
        $params['fee']   = $roomPrice + $params['water_price'] + $params['electricity_price'] + $propertyPrice;
        $params['room_price'] = $roomPrice;
        $params['property_price']  = $propertyPrice;

        $response = $next($params);
        // 生成租户待缴费记录
        $res  = $payRecordLogic->addLesseeRecord($response,$params);
        // halt($response);
        
        return $response;
    }
   
}